package com.master.train.business.controller;

import com.master.train.business.entity.TrainEntity;
import com.master.train.business.service.TrainSeatService;
import com.master.train.business.service.TrainService;
import com.master.train.common.utils.PageUtils;
import com.master.train.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;



/**
 * 车次
 *
 * @author master
 * @email 3221057417@qq.com
 * @date 2025-07-17 16:17:17
 */
@RestController
@RequestMapping("business/admin/train")
public class TrainController {
    @Autowired
    TrainService trainService;

    @Autowired
    TrainSeatService trainSeatService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = trainService.queryPage(params);

        return R.ok().put("page", page);
    }

    /*
    * 查询所有
    * */
    @RequestMapping("/query-all")
    public R listAll(){
        List<TrainEntity> trainEntities = trainService.list();

        return R.ok().put("list", trainEntities);
    }



    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		TrainEntity train = trainService.getById(id);

        return R.ok().put("train", train);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody TrainEntity train){
        Date now = new Date();
        if (train.getId() == null){
            train.setCreateTime(now);
            train.setUpdateTime(now);
        }else {
            train.setUpdateTime(now);
        }
		trainService.saveOrUpdate(train);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody TrainEntity train){
		trainService.updateById(train);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		trainService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }
    @RequestMapping("/gen-seat")
    public R genSeat(@RequestBody String trainCode){
        trainSeatService.genTrainSeat(trainCode);

        return R.ok();
    }

}
